package JackSu.project_model;

/**
 * BuilderTest
 *
 * @author Syzer
 * @Description 建造者模式
 * @Date 2021/7/8 18:16
 */
public class BuilderTest {

    private static class Product {
        private String head;
        private String tail;

        public String getHead() {
            return head;
        }

        public void setHead(String head) {
            this.head = head;
        }

        public String getTail() {
            return tail;
        }

        public void setTail(String tail) {
            this.tail = tail;
        }

        @Override
        public String toString() {
            return "Product{" +
                    "head='" + head + '\'' +
                    ", tail='" + tail + '\'' +
                    '}';
        }
    }

    private static abstract class Builder {
        protected Product product = new Product();

        public abstract void buildHead();

        public abstract void buildTail();

        public Product build() {
            return product;
        }
    }

    private static class SnakeBuilder extends Builder {

        @Override
        public void buildHead() {
            System.out.println("构建贪吃蛇头部");
            product.setHead("头");
        }

        @Override
        public void buildTail() {
            System.out.println("构建贪吃蛇尾部");
            product.setTail("尾");
        }
    }

    private static class RunBuilder {
        private Builder builder;

        public RunBuilder(Builder builder) {
            this.builder = builder;
        }

        public Product construct() {
            builder.buildHead();
            builder.buildTail();
            return builder.build();
        }
    }

    public static void main(String[] args) {
        RunBuilder runBuilder = new RunBuilder(new SnakeBuilder());
        Product construct = runBuilder.construct();
        System.out.println(construct);
    }
}
